<?php

namespace App\Http\Controllers\Innerend;


use App\Models\User;

class SiteFeedbackController extends BaseController
{
    // 话题
    public function initialize()
    {
        parent::initialize();

        $this->model = 'SiteFeedback';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return ['list' => $lists, 'tr' => $trList];
        }

        $userDb = new User();
        $uids   = array_column($lists, 'uid');
        $users  = $userDb->isCache(true)->getListInIds($uids);

        // 展示列表表格
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_uid  = $item['uid'];
            $_user = $users[$_uid] ?? [];
            $_id   = $item['id'];
            //$status   = $item['status'] == 1 ? '正常' : '禁用';
            //$majorOp  = $item['is_major'] == 1 ? '设为不重要' : '设为重要';
            //$is_major = $item['is_major'] == 1 ? '是' : '否';
            // 操作
            //$operate = $listHtml->operate_status(['id' => $_id, 'is_major' => 1 - $item['is_major']], $majorOp, $item['is_major']);
            //$operate .= $listHtml->operate_edit(['id' => $_id], '编辑');
            //$operate = $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该快讯吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($_user['username']);
            $tr .= $listHtml->list_td($item['content']);
            $tr .= $listHtml->list_td($item['ip']);
            //$tr .= $listHtml->list_td($status);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            //$tr .= $listHtml->list_td($operate);
            //$tr .= $operate;

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return ['list' => $lists, 'tr' => $trList];
    }

    // 列表字段
    protected function list_fields()
    {
        // 'operate' => ['操作', 250],
        return ['uid' => ['用户', 80], 'content' => ['反馈内容', 250], 'ip' => ['ip', 40], 'ctime' => ['创建时间', 120],];
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = url('/innerend/'.$this->controller.'/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加</a> ';
        $this->assign('top_button', '');
    }


    // 表单页面
    protected function formTable()
    {
        $html = '';
        $info = $this->onShow();
        $form = $this->adminForm();

        // 用户名
        $username = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        $ip       = $form->formShow($info['ip'] ?? '127.0.0.1');
        $content  = $form->formTextarea('content', $info['content'] ?? '');

        $html .= $form->makeItem('用户', $username);
        $html .= $form->makeItem('ip', $ip);
        $html .= $form->makeItem('内容', $content);

        return $html;
    }

    // 列表查询
    protected function listWhere()
    {
        $keyword = $this->getData('keyword', null, 'trim');
        $where   = [];
        if($keyword){
            $where[] = $this->orWhere('content|reply', 'like', $keyword);
            // 不在缓存
            $this->list_cache = false;
        }

        return $where;
    }

    // 反馈
    public function reply()
    {
        $this->throwIf(empty($this->id), '请指定修改的ID编号！');

        $reply = $this->getData('reply', '', 'trim');
        $this->throwIf(empty($reply), '请填写反馈内容！');

        // 组合数据
        $where = ['id' => $this->id];;
        $data   = ['reply' => $reply, 'is_view' => 1,];
        $model  = $this->checkModel($this->model);
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 添加
    protected function _before_add()
    {
        $data = [
            'uid'     => $this->getData('uid', 0, 'intval'),
            'content' => $this->getData('content', null, 'trim'),
            'is_view' => 1,
            'reply'   => $this->getData('reply', null, 'trim'),
        ];

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            'uid'     => $this->getData('uid', 0, 'intval'),
            'content' => $this->getData('content', null, 'trim'),
            'is_view' => 1,
            'reply'   => $this->getData('reply', null, 'trim'),
        ];

        return $data;
    }

}


